package handler

import (
	"github.com/labstack/echo/v4"
	"log"
	"strconv"
	"time"
	db "ulcdkey/db"
	"ulcdkey/tool"
)

//获取所有渠道列表
func GetChannelList(c echo.Context) error {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)

	channels := new(db.Channel)

	as, err := channels.GetChannelList()
	if err != nil {
		log.Println("GetChannelList:", err)
		r.Code = -1
		r.Msg = "获取渠道列表失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	r.Data = as
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 根据平台获取渠道列表
func GetChannelListByPlatform(c echo.Context) error {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)

	channels := new(db.Channel)
	channels.Platform = tool.GetHeaderPlatform(c)

	as, err := channels.GetChannelListByPlatform()
	if err != nil {
		log.Println("GetChannelListByPlatform:", err)
		r.Code = -1
		r.Msg = "获取渠道列表失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	r.Data = as
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 新增渠道
func AddChannel(c echo.Context) (err error) {
	var r ResponseResult

	t := time.Now()
	uid := tool.GetUid(c)
	channels := new(db.Channel)
	userInfo := tool.GetVisitUserInfo(c)

	copChannelIdStr := c.FormValue("copChannelId")
	channels.ChannelName = c.FormValue("channelName")
	channels.CopChannelId, err = strconv.Atoi(copChannelIdStr)
	if err != nil {
		log.Println("copChannelId参数错误 err:", err)
		r.Code = -1
		r.Msg = "copChannelId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	channels.Platform = tool.GetHeaderPlatform(c)
	err = channels.AddChannel(userInfo)
	if err != nil {
		log.Println("AddChannel err:", err)
		r.Code = -1
		r.Msg = "新增渠道失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 更新渠道
func UpdateChannel(c echo.Context) (err error) {
	var r ResponseResult

	t := time.Now()
	uid := tool.GetUid(c)
	channels := new(db.Channel)
	userInfo := tool.GetVisitUserInfo(c)

	idStr := c.FormValue("id")
	copChannelIdStr := c.FormValue("copChannelId")
	channels.ChannelName = c.FormValue("channelName")

	channels.Id, err = strconv.Atoi(idStr)
	if err != nil {
		log.Println("id参数错误 err:", err)
		r.Code = -1
		r.Msg = "id参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	channels.CopChannelId, err = strconv.Atoi(copChannelIdStr)
	if err != nil {
		log.Println("copChannelId参数错误 err:", err)
		r.Code = -1
		r.Msg = "copChannelId参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}
	channels.Platform = tool.GetHeaderPlatform(c)

	err = channels.UpdateChannel(userInfo)
	if err != nil {
		log.Println("UpdateChannel err:", err)
		r.Code = -1
		r.Msg = "更新渠道失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 删除渠道
func DelChannel(c echo.Context) (err error) {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)
	channels := new(db.Channel)
	userInfo := tool.GetVisitUserInfo(c)

	idStr := c.FormValue("id")

	channels.Id, err = strconv.Atoi(idStr)
	if err != nil {
		log.Println("id参数错误 err:", err)
		r.Code = -1
		r.Msg = "id参数错误,err=" + err.Error()
		return c.JSON(200, r)
	}

	err = channels.DelChannel(userInfo)
	if err != nil {
		log.Println("DelChannel err:", err)
		r.Code = -1
		r.Msg = "删除渠道失败,err=" + err.Error()
		return c.JSON(200, r)
	}
	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}
